package com.itstorm.pomeloweather.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itstorm.pomeloweather.R;
import com.itstorm.pomeloweather.entity.WeatherData;

public class Gallery3DaysAdapter extends BaseAdapter {

	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private List<WeatherData> list;
	private Context context;

	public Gallery3DaysAdapter(Context context, List<WeatherData> weatherDatas) {
		this.context = context;
		this.list = (weatherDatas != null) ? weatherDatas.subList(1,
				weatherDatas.size()) : new ArrayList<WeatherData>();
	}

	public void changeList(List<WeatherData> weatherDatas) {
		this.list = (weatherDatas != null) ? weatherDatas.subList(1,
				weatherDatas.size()) : new ArrayList<WeatherData>();
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = rowViews.get(position);
		if (rowView == null) {
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			rowView = layoutInflater.inflate(R.layout.g3dgallery_item, null);
			TextView tvData = (TextView) rowView.findViewById(R.id.tv_data);
			TextView tvWeather = (TextView) rowView
					.findViewById(R.id.tv_weather);
			TextView tvWind = (TextView) rowView.findViewById(R.id.tv_wind);
			TextView tvTemperature = (TextView) rowView
					.findViewById(R.id.tv_temperature);
			ImageView imgWeather = (ImageView) rowView
					.findViewById(R.id.img_weather);

			WeatherData weatherData = list.get(position);

			if (weatherData.getDate().length() != 0) {
				tvData.setText(weatherData.getDate());
			} else {
				tvData.setText("N/A");
			}
			if (weatherData.getWind().length() != 0) {
				tvWind.setText(weatherData.getWind());
			} else {
				tvWind.setText("N/A");
			}
			if (weatherData.getTemperature().length() != 0) {
				tvTemperature.setText(weatherData.getTemperature());
			} else {
				tvTemperature.setText("N/A");
			}

			if (weatherData.getWeather().length() != 0) {
				tvWeather.setText(weatherData.getWeather());
				if (weatherData.getWeather().contains("晴")) {
					imgWeather.setImageResource(R.drawable.qing);
				} else if (weatherData.getWeather().contains("风")) {
					imgWeather.setImageResource(R.drawable.feng);
				} else if (weatherData.getWeather().contains("霾")) {
					imgWeather.setImageResource(R.drawable.mai);
				} else if (weatherData.getWeather().contains("雪")) {
					imgWeather.setImageResource(R.drawable.xue);
				} else if (weatherData.getWeather().contains("阴")) {
					imgWeather.setImageResource(R.drawable.yin);
				} else if (weatherData.getWeather().contains("云")) {
					imgWeather.setImageResource(R.drawable.yun);
				} else if (weatherData.getWeather().contains("雨")) {
					if (weatherData.getWeather().contains("大雨")
							| weatherData.getWeather().contains("暴雨")) {
						imgWeather.setImageResource(R.drawable.dayu);
					} else if (weatherData.getWeather().contains("中雨")
							| weatherData.getWeather().contains("阵雨")) {
						imgWeather.setImageResource(R.drawable.zhongyu);
					} else {
						imgWeather.setImageResource(R.drawable.xiaoyu);
					}
				}
			} else {
				tvWeather.setText("N/A");
				imgWeather.setImageResource(R.drawable.weather_default);
			}
		}

		return rowView;
	}
}
